package com.zhongxi.guoyuan.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.facetoface.FacePayDataResult;
import com.zhongxi.guoyuan.entity.facetoface.FaceShopInfo;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.DataUtil;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */

public class FacePayOrderAdapter extends BaseAdapter {

    private Context mContext;
    private List<FacePayDataResult.FaceOrderInfo> list;
    private FaceShopInfo faceShopInfo;
    private Handler handler;
    private int mExpandedMenuPos = -1;

    public FacePayOrderAdapter(Context context, List<FacePayDataResult.FaceOrderInfo> list,FaceShopInfo faceShopInfo,Handler handler) {
        this.mContext = context;
        this.list = list;
        this.faceShopInfo=faceShopInfo;
        this.handler=handler;
    }

    public int getmExpandedMenuPos() {
        return mExpandedMenuPos;
    }

    public void setmExpandedMenuPos(int mExpandedMenuPos) {
        this.mExpandedMenuPos = mExpandedMenuPos;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_sliver_order, parent, false);
            holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
            holder.tvOrderId = (TextView) convertView.findViewById(R.id.tvOrderId);
            holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
            holder.tvAward = (TextView) convertView.findViewById(R.id.tvAward);
            holder.tvPay= (TextView) convertView.findViewById(R.id.tvPay);
            holder.tvGift= (TextView) convertView.findViewById(R.id.tvGift);

            holder.text_isOnLine= (TextView) convertView.findViewById(R.id.text_isOnLine);
            holder.ivDown= (ImageView) convertView.findViewById(R.id.iv_down);
            holder.listMenu= (LinearLayout) convertView.findViewById(R.id.linearMenu);
            holder.tvDelete= (TextView) convertView.findViewById(R.id.tv_delete);
            holder.tvPayName= (TextView) convertView.findViewById(R.id.consume_pay_shopname);
            holder.tvPayPhone= (TextView) convertView.findViewById(R.id.consume_pay_shopphone);
            holder.tvPayAddress= (TextView) convertView.findViewById(R.id.consume_pay_address);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        //1 线上  0线下
        try{
            if(null!=list.get(position)&&!"".equals(list.get(position))){

               FacePayDataResult.FaceOrderInfo data = list.get(position);

                if(null!=data.getThumbnail()&&!"".equals(data.getThumbnail())){
                    Glide.with(mContext).load(Url.IMG_URL + data.getThumbnail()).placeholder(R.mipmap.iv_eg).into(holder.ivImg);
                }
                if(null!=data.getGoodsname()&&!"".equals(data.getGoodsname())){
                    holder.tvName.setText(data.getGoodsname());
                }else{
                    holder.tvName.setText("");
                }
                if(null!=data.getId()&&!"".equals(data.getId())){
                    holder.tvOrderId.setText("订单："+data.getId());
                }else{
                    holder.tvOrderId.setText("订单：");
                }
                if(null!=data.getPaytime()&&!"".equals(data.getPaytime())){
                    holder.tvTime.setText("消费时间："+data.getPaytime());
                }else{
                    holder.tvTime.setText("消费时间：");
                }

                if (null!=data.getShouldprice()&&!"".equals(data.getShouldprice())){
                    holder.tvPay.setText("金额："+data.getShouldprice()+"元");
                }else {
                    holder.tvPay.setText("金额：");
                }
                if (null!=data.getBackredbean()&& DataUtil.moreThanZero(data.getBackredbean())){
                    holder.tvAward.setVisibility(View.VISIBLE);
                    holder.tvAward.setText(mContext.getResources().getString(R.string.award_hongdou)+data.getBackredbean()+"红豆");
                }else {
                    holder.tvAward.setText("");
                    holder.tvAward.setVisibility(View.GONE);
                }
                if (null!=data.getBackinnernum()&&DataUtil.moreThanZero(data.getBackinnernum())){
                    holder.tvGift.setVisibility(View.VISIBLE);
                    holder.tvGift.setText(mContext.getResources().getString(R.string.give_meidou)+data.getBackinnernum()+"美豆");
                }else {
                    holder.tvGift.setText("");
                    holder.tvGift.setVisibility(View.GONE);

                }
                if(null!=data.getIsonline()&&!"".equals(data.getIsonline())){
                    if (data.getIsonline().equals("1")){
                        holder.text_isOnLine.setBackground(mContext.getResources().getDrawable(R.drawable.bg_consume_sqround));
                        holder.text_isOnLine.setText("线上");
                    }else {
                        holder.text_isOnLine.setBackground(mContext.getResources().getDrawable(R.drawable.bg_consume_txround));
                        holder.text_isOnLine.setText("线下");
                    }
                }else{
                    holder.text_isOnLine.setText("");
                }

                holder.tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        AlertDialog dialog=new AlertDialog.Builder(mContext).setTitle("您确定要删除该项目吗？")
                                .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Message msg = Message.obtain();
                                        msg.obj = position;
                                        msg.what = 0;
                                        handler.sendMessage(msg);
                                        dialog.dismiss();
                                    }
                                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).show();
                        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(mContext.getResources().getColor(R.color.bg_yield));
                        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(mContext.getResources().getColor(R.color.bg_yield));
                    }
                });

                if(null!=faceShopInfo&&!"".equals(faceShopInfo)){
                    if(null!=faceShopInfo.getName()&&!"".equals(faceShopInfo.getName())){
                        holder.tvPayName.setText(faceShopInfo.getName());
                    }else{
                        holder.tvPayName.setText(faceShopInfo.getName());
                    }
                    
                    if(null!=faceShopInfo.getAddress()&&!"".equals(faceShopInfo.getAddress())){
                        holder.tvPayAddress.setText(faceShopInfo.getAddress());
                    }else{
                        holder.tvPayAddress.setText("");
                    }
                    
                    if(null!=faceShopInfo.getMphone()&&!"".equals(faceShopInfo.getMphone())){
                        holder.tvPayPhone.setText(faceShopInfo.getMphone());
                    }else{
                        holder.tvPayPhone.setText("");
                    }
                }else{
                    holder.tvPayName.setText("");
                    holder.tvPayAddress.setText("");
                    holder.tvPayPhone.setText("");
                }


            }

            holder.ivDown.setImageResource(position == mExpandedMenuPos ? R.mipmap.iv_down_light : R.mipmap.iv_down);
            holder.listMenu.setVisibility(position == mExpandedMenuPos ? View.VISIBLE : View.GONE);
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }



    class ViewHolder{
        private ImageView ivImg;
        private TextView tvOrderId;
        private TextView tvTime;
        private TextView tvName;
        private TextView tvAward;
        private TextView tvGift;
        private TextView tvPay;

        private ImageView ivDown;
        private LinearLayout listMenu;
        private TextView tvDelete;
        private TextView text_isOnLine;

        private TextView tvPayName;
        private TextView tvPayPhone;
        private TextView tvPayAddress;
    }
}
